/*
 * @file yt_bottom_navigation_bar
 * @Author: liuer
 * @Date: 2024/8/8 09:15
 * @LastModified: 2024-08-08 09:15
 * @LastModifiedBy: liuer
 */
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_permission/yt_bottom_bar_model.dart';
import 'package:yt_devops/components/app_permission/yt_tab_view_model.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/event_bus_util/eventbus_util.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/func_utils.dart';

import '../gaps.dart';

class YtBottomNavigationBar extends StatefulWidget {
  final List<YtTabViewModel> bottomBarList;
  final void Function(int, TabController, {YtTabViewModel? data})? onTap;

  ///需要把TabController传递出去，不然会报错
  const YtBottomNavigationBar(this.bottomBarList, this.onTap, {super.key});
  @override
  State<YtBottomNavigationBar> createState() => _YtBottomNavigationBarState();
}

class _YtBottomNavigationBarState extends State<YtBottomNavigationBar>
    with TickerProviderStateMixin {
  int selectedIndex = 0;
  List<BottomNavigationBarItem> items = [];
  List<YtTabViewModel> barItemData = [];
  TabController? tabController;
  StreamSubscription? changeLanguageSubscription;
  @override
  void initState() {
    // TODO: implement initState
    _initItems();
    tabController = TabController(length: items.length, vsync: this);
    changeLanguageSubscription =
        EventBusUtil.getInstance().on<SwitchLanguage>().listen((event) {
      Log.d('YtBottomNavigationBarSwitchLanguage===');
      _initItems(isFirstLoad: false);
    });
    super.initState();
  }

  _initItems({bool isFirstLoad = true}) {
    items.clear();
    barItemData.clear();
    for (var element in widget.bottomBarList) {
      if (FuncUtils.isPassAuthority([element.permissionString])) {
        items.add(_getItem(element.labelString.tr, element.imageString));
        barItemData.add(element);
      }
    }
    if (!isFirstLoad) {
      setState(() {});
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    changeLanguageSubscription?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return (items.isNotEmpty && items.length >= 2)
        ? BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            backgroundColor: Colors.white,
            currentIndex: selectedIndex,
            onTap: onItemTapped,
            selectedItemColor: Colour.primaryColor,
            unselectedItemColor: Colour.lightTextColor,
            items: items)
        : Gaps.empty;
  }

  onItemTapped(index) {
    selectedIndex = index;
    if (widget.onTap != null) {
      widget.onTap!(index, tabController!, data: barItemData[index]);
    }
    setState(() {});
  }

  _getItem(String labelString, String imageString) {
    return BottomNavigationBarItem(
      label: labelString,
      icon: Image.asset(
        imageString,
        width: 24.w,
        height: 24.w,
        color: Colour.bottomTabBarColor,
      ),
      activeIcon: Image.asset(
        imageString,
        color: Colour.primaryColor,
        width: 24.w,
        height: 24.w,
      ),
    );
  }
}
